package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.SysDict;
import com.k2data.k2app.mapper.SysDictMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysDictService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户
 * Created by clb on 17-3-17.
 */
@RestController
@RequestMapping("/dicts")
public class SysDictController {
    private final SysDictMapper sysDictMapper;
    private final SysDictService sysDictService;

    @Autowired
    public SysDictController(SysDictMapper sysDictMapper, SysDictService sysDictService) {
        this.sysDictMapper = sysDictMapper;
        this.sysDictService = sysDictService;
    }

    @ApiOperation(value = "创建")
    @ApiImplicitParam(name = "sysDict", value = "sysDict对象", required = true, dataType = "SysDict", paramType = "body")
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> post(@RequestBody SysDict sysDict) throws Exception {
        return ResponseHelper.success(sysDictService.insert(sysDict));
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> delete(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(sysDictMapper.deleteById(id));
    }

    @ApiOperation(value = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "sysDict", value = "sysDict", required = true, dataType = "SysDict", paramType = "body")
    })
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> update(@PathVariable("id") Long id,
                                                       @RequestBody SysDict sysDict) throws Exception {
        sysDict.setId(id);
        return ResponseHelper.success(sysDictService.update(sysDict));
    }


    @ApiOperation(value = "根据id查询")
    @ApiImplicitParam(name = "id", value = "the value of id", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> get(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(sysDictMapper.get(id));
    }

    @ApiOperation(value = "字典")
    @GetMapping(value = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "typeCn", value = "类型描述", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "label", value = "字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "value", value = "字段回填“ dataType = ”string", paramType = "query")
    })
    public ResponseEntity<CommonResultResponse> getConditions(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "label", required = false) String label,
            @RequestParam(value = "typeCn", required = false) String typeCn,
            @RequestParam(value = "value", required = false) String value
    ) throws Exception {
        int pageNumReal = pageNum == null ? 1 : pageNum;
        int pageSizeReal = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
        int count = sysDictMapper.selectCount(null);

        PageHelper.startPage(pageNumReal, pageSizeReal);
        PageInfo pageInfo = new PageInfo(pageNumReal, pageSizeReal, count);

        return ResponseHelper.success(sysDictService.getList(type,typeCn,label,value), pageInfo);
    }

    @ApiOperation(value = "类型")
    @GetMapping(value = "/types")
    public ResponseEntity<CommonResultResponse> getType() throws Exception {
        return ResponseHelper.success(sysDictMapper.selectTypes());
    }

}
